<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { useUserStore } from '@/store/modules/user';
import auth from '@/utils/auth';

onLaunch(() => {
  console.log("App Launch");
  
  // 检查登录状态
  const userStore = useUserStore();
  const token = uni.getStorageSync('token');
  const userInfo = uni.getStorageSync('userInfo');
  
  if (token && userInfo) {
    userStore.setToken(token);
    userStore.setUserInfo(userInfo);
  }
});

onShow(() => {
  console.log("App Show");
  
  // 检查是否需要跳转到登录页
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  
  if (currentPage && !currentPage.route.includes('login')) {
    if (!auth.isLoggedIn()) {
      uni.reLaunch({
        url: '/pages/login/index'
      });
    }
  }
});

onHide(() => {
  console.log("App Hide");
});
</script>

<style>
/* 全局样式 */
page {
  background-color: #F5F5F5;
}
</style>
